<template>
  <div class="app-container">
    <el-table v-loading="loading" :data="adoptionList">
      <el-table-column label="申请编号" align="center" prop="adoptionId"/>
      <el-table-column label="宠物名称" align="center" prop="petName"/>
      <el-table-column label="申请时间" align="center" prop="applyTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.applyTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <span>{{ getStatusLabel(scope.row.status) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="审批意见" align="center" prop="comment">
        <template slot-scope="scope">
          <span>{{ scope.row.status === '0' ? '等待审批' : scope.row.comment }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-view" @click="handleView(scope.row)">查看</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList"/>

    <!-- 查看对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="宠物名称">
          <span>{{ form.petName }}</span>
        </el-form-item>
        <el-form-item label="联系电话">
          <span>{{ form.phone }}</span>
        </el-form-item>
        <el-form-item label="居住地址">
          <span>{{ form.address }}</span>
        </el-form-item>
        <el-form-item label="申请理由">
          <span>{{ form.reason }}</span>
        </el-form-item>
        <el-form-item label="申请时间">
          <span>{{ parseTime(form.applyTime) }}</span>
        </el-form-item>
        <el-form-item label="状态">
          <span>{{ getStatusLabel(form.status) }}</span>
        </el-form-item>
        <el-form-item v-if="form.status !== '0'" label="审批意见">
          <span>{{ form.comment }}</span>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { listMyAdoption, getAdoption } from "@/api/user/adoption";

export default {
  name: "MyAdoption",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 领养申请列表
      adoptionList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10
      },
      // 表单参数
      form: {},
      // 状态选项
      statusOptions: [
        { value: "0", label: "待审核" },
        { value: "1", label: "已通过" },
        { value: "2", label: "已拒绝" }
      ]
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询领养申请列表 */
    getList() {
      this.loading = true;
      listMyAdoption(this.queryParams).then(response => {
        this.adoptionList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查看按钮操作 */
    handleView(row) {
      getAdoption(row.adoptionId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "查看领养申请";
      });
    },
    /** 获取状态标签 */
    getStatusLabel(status) {
      const found = this.statusOptions.find(option => option.value === status);
      return found ? found.label : '';
    }
  }
};
</script>
